//@ sourceURL=/static/modules/employee_manage/employee_add.js
var employeeAddView = new View({
	name: "employee_add",
	init_page() {
		if (employeeAddView.type === 'edit') {
			$('.d-password').remove();
			$('.d-password2').remove();
			$('input[name=userName]').attr('readonly', 'readonly');
			//设置角色选中
			if (userVO.roleIds) {
				userVO.roleIds.forEach(function (id) {
					$('#role-' + id).iCheck('check');
				})
			}

			//状态
			$('[name=status][value=0]').attr('checked', false);
			$('[name=status][value=1]').attr('checked', false);
			$('[name=status][value=' + userVO.status + ']').click();

			$('#employee_add_container .page-title').html('编辑员工');
		} else {
			$('#employee_add_container .page-title').html('新建员工');
		}
	}
});


employeeAddView.use(new Form({
	selector: "#employee_add_form",
	type: 'add',
	url: employeeAddView.type == 'edit' ? 'user/editSave' : "/user/addSave",
	before_callback: function (data) {
		// if (!data.roleIds || data.roleIds.length == 0) {
		// 	alertify.error('请选择角色');
		// 	data = false;
		// }
		return data;
	},
	success: function (data) {
		if (employeeAddView.type == 'edit') {
			alertify.success('员工修改成功!');
		} else {
			alertify.success('员工添加成功!');
		}
		// pjaxInit('/workshop?key=employee_manage');
		router.to('employee_manage');
	},
	error: function () {
		if (employeeAddView.type == 'edit') {
			alertify.error('员工修改失败!');
		} else {
			alertify.error('员工添加失败!');
		}

	}
}));

/*
部门管理tree  选择部门
 */
employeeAddView.use(new Tree({
	name: "selected_employee_tree",
	selector: "#selected_employee_container",
	data: departmentTree,
	check_callback: true,
	delay: true,
	select_callback: function (data) {
		//点击节点
		if (data.node.parent && data.node.parent != '#') {
			var node = data.node;
			var nodeId = node.id;
			var nodeName = node.text;
			var parentId = node.parent;
			employeeAddView.plugins['selected_employee_dialog'].close();
			$('input[name=departmentName]').val(nodeName);
			$('input[name=deptId]').val(nodeId);
			$('input[name=departmentName]').trigger('input');
		}

	},
	change_callback: function (data) {
		u.log(2);
	},
	load_callback: function (e, data) {
		u.log(3);
	}
}));

/**
 * 初始省市联动插件
 */
employeeAddView.use(new AreaSelected({
	selector: '#employee_add_form .form-inner',
	url: '/area/data',
	params: {}
}))
/**
 * 地图插件
 */
employeeAddView.use(new Map({
	selector: 'map',
	longitude: 39.866229,
	latitude: 116.307149,
	message: '北京华数康数据科技有限公司'
}))
// 图片上传
$('#btn-up-img').click(() => {
	let imgUp = new ImgUp({
		selector: '#employee_add_container',
		url: ''
	})
	imgUp.init();
})
